//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (S) 2008-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

package e.odbo.data.model;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Column {

    private String name;

    private ColumnType columnType;

    private ColumnFlag columnFlag=ColumnFlag.build(ColumnFlag.BASE);

    private String refTable=null;

    private String refColumn=null;

    private ForeignKeyFlag refCascade_del=ForeignKeyFlag.NO_SET;

    private ForeignKeyFlag refCascade_update=ForeignKeyFlag.NO_SET;

    private String comment;

    private Map<String,ColumnType> dialectTypeMap=new HashMap<>();

    public Column(String name,ColumnType columnType) {
        this.name = name;
        this.columnType=columnType;
    }

    public Column(String name,ColumnType columnType,ColumnFlag columnFlag) {
        this.name = name;
        this.columnType=columnType;
        this.columnFlag=columnFlag;
    }

    public static Column c(String name, ColumnType columnType){
        return new Column( name, columnType);
    }

    public static Column c(String name, ColumnType columnType, String comment){
        return new Column( name, columnType).comment(comment);
    }

    public static Column c(String name, ColumnType columnType, ColumnFlag columnFlag){
        return new Column( name, columnType, columnFlag);
    }

    public static Column c(String name, ColumnType columnType, ColumnFlag columnFlag, String comment){
        return new Column( name, columnType, columnFlag).comment(comment);
    }

    public static Column name(String name){
        return new Column(name,ColumnType.VARCHAR);
    }

    public Column type(ColumnType type){
        this.columnType=type;
        return this;
    }

    public String getName() {
        return name;
    }

    public ColumnType getColumnType() {
        return columnType;
    }

    public ColumnFlag getColumnFlag() {
        return columnFlag;
    }

    public ColumnType getDialectColumnType(String type){
        ColumnType ret= dialectTypeMap.get(type);
        return ret;
    }

    public Collection<ColumnType> getDialectColumnTypes(){
        if(dialectTypeMap.isEmpty())
            return null;
        return dialectTypeMap.values();
    }

    public Column ref(String table,String column){
        this.refTable=table;
        this.refColumn=column;
        return this;
    }

    public Column refCascade(String table,String column){
        this.refTable=table;
        this.refColumn=column;
        this.refCascade_del=ForeignKeyFlag.CASCADE;
        this.refCascade_update=ForeignKeyFlag.CASCADE;
        return this;
    }

    public Column ref(String table,String column,ForeignKeyFlag refCascade_del,ForeignKeyFlag refCascade_update){
        this.refTable=table;
        this.refColumn=column;
        this.refCascade_del=refCascade_del;
        this.refCascade_update=refCascade_update;
        return this;
    }

    public Column PK(){
        this.columnFlag.and(ColumnFlag.PK);
        return this;
    }

    public Column NotNull(){
        this.columnFlag.and(ColumnFlag.NotNull);
        return this;
    }

    public Column AutoGreater(){
        this.columnFlag.and(ColumnFlag.AutoGreater);
        return this;
    }

    public Column Index(){
        this.columnFlag.and(ColumnFlag.Index);
        return this;
    }

    public Column Unique(){
        this.columnFlag.and(ColumnFlag.Unique);
        return this;
    }

    public Column comment(String comment){
        this.comment=comment;
        return this;
    }

    public String getComment(){
        return this.comment;
    }

    public Column dialectType(ColumnType ...types){
        for(ColumnType type:types)
            this.dialectTypeMap.put(type.dialectName,type);
        return this;
    }

    public boolean isRefColumn(){
        return this.refTable!=null&&this.refColumn!=null;
    }

    public String getRefTable() {
        return refTable;
    }

    public String getRefColumn() {
        return refColumn;
    }

    public boolean isRefCascade() {
        return refCascade_del!=ForeignKeyFlag.NO_SET;
    }

    public ForeignKeyFlag getRefCascade_del() {
        return refCascade_del;
    }

    public ForeignKeyFlag getRefCascade_update() {
        return refCascade_update;
    }

    public boolean isPK(){
        return ColumnFlag.isPK(columnFlag);
    }

    public boolean isNoNull(){
        return ColumnFlag.isNotNull(columnFlag);
    }

    public boolean isAutoGreater(){
        return ColumnFlag.isAutoGreater(columnFlag);
    }
}
